package com.ruoyi.points.pointsManage.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.points.pointsManage.mapper.PointsMapper;
import com.ruoyi.points.pointsManage.domain.Points;
import com.ruoyi.points.pointsManage.service.IPointsService;

import javax.annotation.Resource;

/**
 * 积分管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-07
 */
@Service
public class PointsServiceImpl implements IPointsService 
{
    @Autowired
    private PointsMapper pointsMapper;

    /**
     * 查询积分管理
     * 
     * @param id 积分管理主键
     * @return 积分管理
     */
    @Override
    public Points selectPointsById(Long id)
    {
        return pointsMapper.selectPointsById(id);
    }

    /**
     * 查询积分管理列表
     * 
     * @param points 积分管理
     * @return 积分管理
     */
    @Override
    public List<Points> selectPointsList(Points points)
    {
        List<Points> list = pointsMapper.selectPointsList(points);
        if (points.getNickName() != null) {
            List<Points> list1 = new ArrayList<Points>();
            for (Points i : list) {
                if (i.getNickName().contains(points.getNickName())) {
                    list1.add(i);
                }
            }
            return list1;
        }
        return list;
    }

    /**
     * 新增积分管理
     * 
     * @param points 积分管理
     * @return 结果
     */
    @Override
    public int insertPoints(Points points)
    {
        return pointsMapper.insertPoints(points);
    }

    /**
     * 修改积分管理
     * 
     * @param points 积分管理
     * @return 结果
     */
    @Override
    public int updatePoints(Points points)
    {
        return pointsMapper.updatePoints(points);
    }

    /**
     * 批量删除积分管理
     * 
     * @param ids 需要删除的积分管理主键
     * @return 结果
     */
    @Override
    public int deletePointsByIds(Long[] ids)
    {
        return pointsMapper.deletePointsByIds(ids);
    }

    /**
     * 删除积分管理信息
     * 
     * @param id 积分管理主键
     * @return 结果
     */
    @Override
    public int deletePointsById(Long id)
    {
        return pointsMapper.deletePointsById(id);
    }

    @Override
    public Points selectByUserId(Long userId) {
        return pointsMapper.selectPointsByUserId(userId);
    }

    @Override
    public int increasePoints(Long userId, int quantity) {
        Points points = selectByUserId(userId);
        if (quantity < 0 && points.getPoints() < quantity) {
            return -1;
        }
        points.setPoints(points.getPoints() + quantity);
        return updatePoints(points);
    }
}
